package com.example.springaop.aop.service;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.dingtalk.open.app.api.OpenDingTalkClient;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.callback.OpenDingTalkCallbackListener;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.taobao.api.ApiException;

import java.util.Arrays;

/**
 * 测试回调
 *
 * @author: 任福祥
 * * @create: 2024-01-26 15:56
 */
public class BotExample {

    private static final String clientId = "dinggwxiesku6toq9rsh";
    private static final String clientSecret = "xx8K2OkZXN-ef_0eLMtgQHQHJp1MQy55Uzelojr8yYcy2kOXScHxmeM4Q5ZdxIn1";
    private static final String topic = "/v1.0/im/bot/messages/get";

    public static void main(String[] args) throws Exception {
        OpenDingTalkClient client = OpenDingTalkStreamClientBuilder
                .custom()
                .credential(new AuthClientCredential(clientId, clientSecret))
                .registerCallbackListener(topic, new RobotMsgCallbackConsumer())
                .build();
        client.start();
    }


    public static class RobotMsgCallbackConsumer implements OpenDingTalkCallbackListener<JSONObject, JSONObject> {

        /*
         * @param request
         * @return
         */
        @Override
        public JSONObject execute(JSONObject request) {
            try {
                JSONObject text = request.getJSONObject("text");
                if (text != null) {
                    String msg = text.getString("content").trim();
                    System.out.println(msg);
                    String openConversationId = request.getString("conversationId");


//                    service(msg);
                }
            } catch (Exception e) {
                System.out.println("receive group message by robot error:" +e.getMessage());
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name","张三");
            return jsonObject;
        }

        public static void service(String msg){
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=ab0516bfc93084c051d4e08a31c09f68e9f264edf63f782a7f84f65e64ecc6bf");
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(msg);
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(Arrays.asList("132xxxxxxxx"));
// isAtAll类型如果不为Boolean，请升级至最新SDK
            at.setIsAtAll(true);
            at.setAtUserIds(Arrays.asList("109929","32099"));
            request.setAt(at);
            try {
                OapiRobotSendResponse response = client.execute(request);
                System.out.println(JSONObject.toJSONString(response));
            } catch (ApiException e) {
                throw new RuntimeException(e);
            }
        }
    }
}